﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScheduleMaker.DB.Domain;
using ScheduleMaker.DB.Domain.Core;

namespace ScheduleMaker.DB.CoreDAO
{
    public class ClassroomDAO : CoreAbstractDAO<Classroom>
    {
        private AdapterAbstractDAO<Classroom, ClassroomSchool> wrappedAbstractDAOSchool;
        private AdapterAbstractDAO<Classroom, ClassroomUniversity> wrappedAbstractDAOUniversity;
        private ClassroomDAOSchool wrappedDAOSchool;
        private ClassroomDAOUniversity wrappedDAOUniversity;

        public ClassroomDAO(ClassroomDAOSchool wrappedDAOSchool)
        {
            this.wrappedDAOSchool = wrappedDAOSchool;
            this.wrappedAbstractDAOSchool =
                new AdapterAbstractDAO<Classroom, ClassroomSchool>((AbstractDAO<ClassroomSchool>)wrappedDAOSchool);
        }

        public ClassroomDAO(ClassroomDAOUniversity wrappedDAOUniversity)
        {
            this.wrappedDAOUniversity = wrappedDAOUniversity;
            this.wrappedAbstractDAOUniversity =
                new AdapterAbstractDAO<Classroom, ClassroomUniversity>((AbstractDAO<ClassroomUniversity>)wrappedDAOUniversity);
        }

        public IList<Classroom> getClassroomsBySubject(Subject subject)
        {
            if (wrappedDAOSchool != null)
            {
                List<ClassroomSchool> list = (List<ClassroomSchool>)wrappedDAOSchool.getClassroomsBySubject(subject);
                return list.ConvertAll(x => (Classroom)x);
            }
            else if (wrappedDAOUniversity != null)
            {
                List<ClassroomUniversity> list = (List<ClassroomUniversity>)wrappedDAOUniversity.getClassroomsBySubject(subject);
                return list.ConvertAll(x => (Classroom)x);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassroomDAOSchool nor ClassroomDAOUniversity was initialized");
            }
        }

        public override void update(Classroom entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.update(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.update(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassroomDAOSchool nor ClassroomDAOUniversity was initialized");
            }
        }

        public override void save(Classroom entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.save(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.save(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassroomDAOSchool nor ClassroomDAOUniversity was initialized");
            }
        }

        public override void saveOrUpdate(Classroom entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.saveOrUpdate(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.saveOrUpdate(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassroomDAOSchool nor ClassroomDAOUniversity was initialized");
            }
        }

        public override void delete(Classroom entity)
        {
            if (wrappedDAOSchool != null)
            {
                wrappedAbstractDAOSchool.delete(entity);
            }
            else if (wrappedDAOUniversity != null)
            {
                wrappedAbstractDAOUniversity.delete(entity);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassroomDAOSchool nor ClassroomDAOUniversity was initialized");
            }
        }

        public override Classroom getById(int id)
        {
            Classroom entity;
            if (wrappedDAOSchool != null)
            {
                entity = wrappedAbstractDAOSchool.getById(id);
            }
            else if (wrappedDAOUniversity != null)
            {
                entity = wrappedAbstractDAOUniversity.getById(id);
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassroomDAOSchool nor ClassroomDAOUniversity was initialized");
            }

            return entity;
        }

        public override IList<Classroom> getAll()
        {
            IList<Classroom> records;
            if (wrappedDAOSchool != null)
            {
                records = wrappedAbstractDAOSchool.getAll();
            }
            else if (wrappedDAOUniversity != null)
            {
                records = wrappedAbstractDAOUniversity.getAll();
            }
            else
            {
                throw new NotImplementedException("Neither wrapped ClassroomDAOSchool nor ClassroomDAOUniversity was initialized");
            }

            return records;
        }
    }
}
